import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeRouter from '../views/home/router'
import Main from '../views/home/main'
Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Main',
    component: Main,
    redirect: "/index",
    children: [
      ...HomeRouter
    ]
  }
  // , {
  //   path: '/Tab',
  //   name: 'Tab',
  //   component: Tab,
  //   redirect:"/tab",
  //   children: [
  //     ...HomeRouter
  //   ]
  // }
]

const router = new VueRouter({
  routes
})


const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  if (typeof (location) == "string") {
    var Separator = "&";
    if (location.indexOf('?') == -1) {
      Separator = '?';
    }
    location = location + Separator + "random=" + Math.random();
  }
  return routerPush.call(this, location).catch(error => error)
}


router.beforeEach((to, from, next) => {
  var token = localStorage.getItem('token')
  if (!token) {
    if (to.name === 'login') {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }

})

export default router